/**********************************************************************
Compile : gcc -O3 -o generate2 generate.c main2.c
Usage   : ./generate2 mode mode1 mode2 N L ratio1/NL ratio2 M pnflag seed
mode    : 0 normal ; 1 cycled
mode1   : 0 disallow self-activation; 1 allow self-activation
mode2   : Additional Requirements: 0 no requirements; 1 restriction I; 2 restriction II; 3 restriction III
N       : number of the nodes
L       : number of the steps
ratio1  : number of links / (N * N)   when <= 1.0
NL      : number of links per node    when >= 2.0
ratio2  : number of red links / number of green links
M       : the number of the attempts
pnflag  : k for one network, print up to k processesa ; 0 unlimited
seed    : random seed
***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "generate.h"

int main (int argc, char *argv[])
{
	int N, L, M, mode, mode1, mode2, pnflag, counter;
	float ratio, ratio2;
	long int seed;

	mode = atoi (argv[1]);
	mode1 = atoi (argv[2]);
	mode2 = atoi (argv[3]);
	N = atoi (argv[4]);
	L = atoi (argv[5]);
	ratio = atof (argv[6]);
	if (ratio >= 2.0)
		ratio = ratio / (float) N;
	ratio2 = atof (argv[7]);
	if (mode1 == 1)
		ratio2 = (ratio * ratio2 / (ratio2 + 1.0) - 1.0 / N) / (ratio / (ratio2 + 1.0) - 1.0 / N);
	else
		ratio2 = (ratio * ratio2 / (ratio2 + 1.0) - (1.0 + (ratio * N - 2.0) / (N - 1.0))/ N) / (ratio / (ratio2 + 1.0) - 1.0 / N);

	ratio = (ratio * N - 2.0) / (N - 2.0);
	M = atoi (argv[8]);
	pnflag = atoi (argv[9]);
	seed = atoi (argv[10]);
	srand48 (seed);

	int i, j, k, l, p, x, y, n, nR, nG;;
	int network[N][N], pathway[L][N];

	n = 1;

	for (i = 0; i < N; i++)
	{
		n *= 2;
	}

	struct state state[n];

	init_state (state, n, N);

	for (i = 0; i < M; i++)
	{
		generate_network (N, mode, mode1, network, ratio, ratio2, &nR, &nG);

		for (j = 0; j < n; j++)
		{
			state[j].status2 = dynamics (N, state[j].status, network);
		}

		for (j = 0; j < n; j++)
		{
			state[j].stable = check_stable (N, j, state, network, mode2);
			state[j].pnflag = 0;
		}

		check_final (N, n, state);

		counter = 0;
		for (j = 0; j < n; j++)
		{
			if (state[j].length != L - 1)
				continue;

			if (mode == 1 && state[j].flag == 3 && state[j].pnflag == 0)
			{
				get_process (N, L, pathway, state, j);
				if (check_process (N, L, pathway) == 1)
				{
					print_process (N, L, pathway, mode, -1, i);
					counter ++;
					if (counter == pnflag)
						break;
				}
			}
			else if (mode == 0 && (state[j].flag == 0 || state[j].flag == 1))
			{
				get_process (N, L, pathway, state, j);
				if (L > 1 && check_process (N, L, pathway) == 1)
				{
					print_process (N, L, pathway, mode, -1, i);
					counter ++;
					if (counter == pnflag)
						break;
				}
			}
		}
	}
}
